﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using VehicleMaintenance.Framework;
using VehicleMaintenance.Models;
using VehicleMaintenance.Comm;
using Microsoft.Reporting.WinForms;

namespace VehicleMaintenance.Transaction
{
    public partial class SettleForm : BaseForm
    {

        //[System.Runtime.InteropServices.DllImport("gdi32.dll")]
        //public static extern long BitBlt(IntPtr hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, int dwRop);

        //private Bitmap memoryImage;

        private int _iCurrentPageIndex = 0;

        private string mainBillCode;

        public SettleForm()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="billCode">主单号</param>
        public SettleForm(string billCode)
        {
            InitializeComponent();

            mainBillCode = billCode;
        }

        private void SettleForm_Load(object sender, EventArgs e)
        {
            try
            {
                CarDBContext db = new CarDBContext();
                MainBill mainBill = db.MainBill.Include("Car").First(p => p.MainBillCode == mainBillCode);

                var bills = db.RecipientsBill.Include("Good").Where(p => p.MainBillCode == mainBillCode).ToList();
                ReportParameter p1 = new ReportParameter("UnitName", db.BaseInfo.First(p => p.ID > 0).Name);
                ReportParameter p2 = new ReportParameter("BeginDate", mainBill.BeginDate);
                ReportParameter p3 = new ReportParameter("CarType", db.CarType.Single(p => p.CarTypeID == mainBill.Car.CarTypeID).Name);
                ReportParameter p4 = new ReportParameter("CarNO", mainBill.CarNO);
                ReportParameter p5 = new ReportParameter("Reception", "");
                ReportParameter p6 = new ReportParameter("Category", db.MaintenanceCategories.First(p => p.CategoryID == mainBill.CategoryID).Name);
                ReportParameter p7 = new ReportParameter("TotalPrice", "ceshi");
                ReportParameter p8 = new ReportParameter("Telephone", db.BaseInfo.First(p => p.ID > 0).TelePhone);
                this.reportViewer1.LocalReport.SetParameters(new ReportParameter[] { p1, p2, p3, p4, p5, p6, p7, p8 });
                //格式化数据源，用来交叉显示数据，根据字段ID
                if (bills != null)
                {
                    while (bills.Count < 16)
                    {
                        bills.Add(new RecipientsBill());
                    }
                }
                int i = 0;
                foreach (var bill in bills)
                {
                    bill.ID = i;
                    i++;
                }
                
                this.PurchaseBillBindingSource.DataSource = bills;
                this.reportViewer1.RefreshReport();
            }
            catch (Exception ex)
            {
                this.ShowInfo(ex.Message);
            }
        }

        private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            ////e.Graphics.DrawImage(memoryImage, 100, 50);
            //// Set the page margins
            //Rectangle rPageMargins = new Rectangle(e.MarginBounds.Location, e.MarginBounds.Size);

            //// Generate the offset origins for the printing window
            //Point[] ptOffsets = Print.GeneratePrintingOffsets(this, rPageMargins);

            //// Make sure nothing gets printed in the margins
            //e.Graphics.SetClip(rPageMargins);

            //// Draw the rest of the Form using the calculated offsets
            //Point ptOffset = new Point(-ptOffsets[_iCurrentPageIndex].X, -ptOffsets[_iCurrentPageIndex].Y);
            //ptOffset.Offset(rPageMargins.X, rPageMargins.Y);
            ////DrawForm(e.Graphics, ptOffset);

            //Print.DrawControl(this.groupBox1, ptOffset, e.Graphics);
        }


        private void btnPrint_Click(object sender, EventArgs e)
        {
            try
            {
                //System.Drawing.Printing.PageSettings pageSetting = new System.Drawing.Printing.PageSettings();


                ////System.Drawing.Printing.PaperSize pageSize = new System.Drawing.Printing.PaperSize("A4", 800, 1100);//设置纸张为A4纸
                ////pageSetting.PaperSize = pageSize;
                //string papername = "A4";

                //foreach (System.Drawing.Printing.PaperSize size in this.printDocument1.PrinterSettings.PaperSizes)
                //{
                //    if (papername == size.PaperName)//看该打印机是否有我们需要的纸张类型 
                //    {
                //        pageSetting.PaperSize = size;
                //    }
                //}

                //pageSetting.Landscape = true;//横向打印     
                //printDocument1.OriginAtMargins = true;
                ////pageSetting.Margins = new System.Drawing.Printing.Margins(35, 35, 20, 20);//设置页边距
                //pageSetting.Margins = new System.Drawing.Printing.Margins((pageSetting.PaperSize.Height - this.tableLayoutPanel1.Width) / 4, (pageSetting.PaperSize.Height - this.tableLayoutPanel1.Width) / 4,
                //(pageSetting.PaperSize.Width - this.tableLayoutPanel1.Height - 100) / 4, (pageSetting.PaperSize.Width - this.tableLayoutPanel1.Height - 100) / 4);//设置页边距
                //printDocument1.DefaultPageSettings = pageSetting;
                ////CaptureScreen(this.tableLayoutPanel1);
                //printPreviewDialog1.AutoSize = true;
                //printPreviewDialog1.PrintPreviewControl.AutoZoom = false;
                //printPreviewDialog1.PrintPreviewControl.Zoom = 1.0;

                //((Form)printPreviewDialog1).WindowState = FormWindowState.Maximized;
                //printPreviewDialog1.ShowDialog(this);

                reportViewer1.PrintDialog();
            }
            catch (Exception ex)
            {
                this.ShowInfo(ex.Message);
            }
        }

        //private void CaptureScreen(Control con)
        //{
        //    try
        //    {
        //        Graphics mygraphics = con.CreateGraphics();
        //        Size s = con.Size;
        //        memoryImage = new Bitmap(s.Width, s.Height, mygraphics);
        //        Graphics memoryGraphics = Graphics.FromImage(memoryImage);
        //        IntPtr dc1 = mygraphics.GetHdc();
        //        IntPtr dc2 = memoryGraphics.GetHdc();
        //        BitBlt(dc2, 0, 0, con.ClientRectangle.Width, con.ClientRectangle.Height, dc1, 0, 0, 13369376);
        //        mygraphics.ReleaseHdc(dc1);
        //        memoryGraphics.ReleaseHdc(dc2);
        //    }
        //    catch (Exception ex)
        //    {
        //        throw ex;
        //    }
        //}

        ///// <summary>
        ///// Draws the Form on the specified Graphics object at the specified offset.
        ///// </summary>
        //private void DrawForm(Graphics g, Point ptOffset)
        //{            
        //    Print.DrawControl(this.groupBox1, ptOffset, g);
        //}


        private void btnSettle_Click(object sender, EventArgs e)
        {
            try
            {
                CarDBContext db = new CarDBContext();
                MainBill bill = db.MainBill.First(p => p.MainBillCode == mainBillCode);
                bill.IsSettle = 1;
                var list = db.RecipientsBill.Where(p => p.MainBillCode == mainBillCode);
                foreach (var item in list)
                {
                    item.IsSettle = 1;
                }
                db.SaveChanges();
                this.ShowInfo("结算完成！");
                this.DialogResult = System.Windows.Forms.DialogResult.OK;
            }
            catch (Exception ex)
            {
                this.ShowInfo(ex.Message);
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            try
            {
                this.Close();
            }
            catch (Exception ex)
            {
                this.ShowInfo(ex.Message);
            }
        }

    }
}
